package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.Service.BookService;
import com.example.demo.commom.Result;
import com.example.demo.entity.Book;
import com.example.demo.entity.BookWithUser;
import com.example.demo.entity.BookWithUser;
import com.example.demo.entity.User;
import com.example.demo.mapper.BookWithUserMapper;
import com.example.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/bookwithuser")
public class BookWithUserController {
    @Resource
    BookWithUserMapper BookWithUserMapper;

    @Resource
    UserMapper userMapper;

    @Autowired
    private BookService bookService;
//
//    @PostMapping
//    public Result<?> save(@RequestBody Book Book){
//        BookMapper.insert(Book);
//        return Result.success();
//    }

    //    //    批量删除
//    @PostMapping("/deleteBatch")
//    public  Result<?> deleteBatch(@RequestBody List<Integer> ids){
//        BookMapper.deleteBatchIds(ids);
//        return Result.success();
//    }
//    @PutMapping
//    public  Result<?> update(@RequestBody Book Book){
//        BookMapper.updateById(Book);
//        return Result.success();
//    }
//    @DeleteMapping("/{id}")
//    public Result<?> delete(@PathVariable Long id){
//        BookMapper.deleteById(id);
//        return Result.success();
//    }
    @PostMapping("/insertNew")
    public Result<?> insertNew(@RequestBody BookWithUser BookWithUser) {
        BookWithUserMapper.insert(BookWithUser);
        return Result.success();
    }

    @PostMapping
    public Result<?> update(@RequestBody BookWithUser BookWithUser) {
        UpdateWrapper<BookWithUser> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("isbn", BookWithUser.getIsbn()).eq("id", BookWithUser.getId());
        BookWithUserMapper.update(BookWithUser, updateWrapper);
        return Result.success();
    }

    //删除一条记录
    @PostMapping("/deleteRecord")
    public Result<?> deleteRecord(@RequestBody BookWithUser BookWithUser) {
        Map<String, Object> map = new HashMap<>();
        map.put("isbn", BookWithUser.getIsbn());
        map.put("id", BookWithUser.getId());
        BookWithUserMapper.deleteByMap(map);
        return Result.success();
    }

    @PostMapping("/deleteRecords")
    public Result<?> deleteRecords(@RequestBody List<BookWithUser> BookWithUsers) {
        int len = BookWithUsers.size();
        for (int i = 0; i < len; i++) {
            BookWithUser curRecord = BookWithUsers.get(i);
            Map<String, Object> map = new HashMap<>();
            map.put("isbn", curRecord.getIsbn());
            map.put("id", curRecord.getId());
            BookWithUserMapper.deleteByMap(map);
        }
        return Result.success();
    }

    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search1,
                              @RequestParam(defaultValue = "") String search2,
                              @RequestParam(defaultValue = "") String search3
    ) {
        LambdaQueryWrapper<BookWithUser> wrappers = Wrappers.<BookWithUser>lambdaQuery();
        if (StringUtils.isNotBlank(search1)) {
            wrappers.like(BookWithUser::getIsbn, search1);
        }
        if (StringUtils.isNotBlank(search2)) {
            wrappers.like(BookWithUser::getBookName, search2);
        }
        if (StringUtils.isNotBlank(search3)) {
            wrappers.like(BookWithUser::getId, search3);
        }

        Page<BookWithUser> BookPage = BookWithUserMapper.selectPage(new Page<>(pageNum, pageSize), wrappers);
        return Result.success(BookPage);
    }

    //cy续借
    @PostMapping("/renew")
    public Result<?> renewBook(@RequestBody BookWithUser bookWithUser) {
        //查询当前借阅记录
        LambdaQueryWrapper<BookWithUser> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(BookWithUser::getIsbn, bookWithUser.getIsbn())
                .eq(BookWithUser::getId, bookWithUser.getId());
        BookWithUser currentRecord = BookWithUserMapper.selectOne(queryWrapper);

        if (currentRecord == null) {
            return Result.error("404", "借阅记录不存在");
        }

        //检查是否逾期
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime deadLine = currentRecord.getDeadtime().toInstant()
                .atZone(ZoneId.systemDefault())
                .toLocalDateTime();

        if (now.isAfter(deadLine)) {
            //逾期检查用户是否已付款
            User user = userMapper.selectById(bookWithUser.getId());
            if (user.getBorrowingStatus() == 1) { // 1表示不可借书(未付款)
                return Result.error("403", "图书已逾期，需先付款才能续借");
            }
        }

        //执行续借逻辑
        UpdateWrapper<BookWithUser> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("isbn", bookWithUser.getIsbn())
                .eq("id", bookWithUser.getId());

        //更新续借次数和截止日期(例如续借30天)
        BookWithUser updateData = new BookWithUser();
        updateData.setProlong(currentRecord.getProlong() + 1);
        updateData.setDeadtime(
                Date.from(deadLine.plusDays(30).atZone(ZoneId.systemDefault()).toInstant())
        );

        BookWithUserMapper.update(updateData, updateWrapper);

        //更新图书表中的借阅次数
        Book book = bookService.getByIsbn(bookWithUser.getIsbn());
        book.setBorrownum(book.getBorrownum() + 1);
        bookService.updateById(book);

        return Result.success();
    }
}
